package com.cosina.mr;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.cosina.mr.dao.DAOFactory;
import com.cosina.mr.dao.IMoneyRecordDAO;
import com.cosina.mr.model.MoneyEvent;
import com.cosina.mr.part.EventNameSpinner;

public class EditRecordActivity extends Activity {
		private EventNameSpinner nameSpinner;
		private EditText idET, descET, moneyET, dateET;
		private Button submitButton;
		
		@Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        this.setContentView(R.layout.editrecord);
	        
	        long id = this.getIntent().getLongExtra(MoneyEvent.ID, -1);
	        Cursor  cursor = DAOFactory.getMRDAO(this).findBy(id);
	        cursor.moveToFirst();
	        
	        idET = (EditText)findViewById(R.id.edit_id);
	    
	        
	        nameSpinner = (EventNameSpinner)findViewById(R.id.edit_name);
	        descET = (EditText)findViewById(R.id.edit_desc);
	        moneyET = (EditText)findViewById(R.id.edit_money);
	        dateET = (EditText)findViewById(R.id.edit_date);
	        submitButton = (Button)findViewById(R.id.edit_submit);
	        
	        submitButton.setOnClickListener(listener);
	        
	        if(id == -1){
	        	findViewById(R.id.edit_id_section).setVisibility(View.INVISIBLE);
	        }else
	        {
	        	idET.setText(id + "");
	        	moneyET.setText(cursor.getString(cursor.getColumnIndexOrThrow(MoneyEvent.MONEY)));
	        	String name = cursor.getString(cursor.getColumnIndexOrThrow(MoneyEvent.NAME));
	        	nameSpinner.setSelection(name);
	        	dateET.setText(cursor.getString(cursor.getColumnIndexOrThrow(MoneyEvent.OCCURTIME)));
	        	descET.setText(cursor.getString(cursor.getColumnIndexOrThrow(MoneyEvent.DESC)));
	        }
	    }
		
		OnClickListener listener = new OnClickListener(){

			@Override
			public void onClick(View v) {
				if(v == submitButton){
					String name = nameSpinner.getSelectedItem().toString();
					if(0 == moneyET.getText().length()){
						Toast.makeText(EditRecordActivity.this, "money can't be null", Toast.LENGTH_SHORT).show();
						return;
					}
					long money = Long.valueOf(moneyET.getText().toString());
					String date = dateET.getText().toString();
					
					MoneyEvent editEvent = new MoneyEvent(idET.getText().toString(),name,descET.getText().toString(), money,date);
					IMoneyRecordDAO dao = DAOFactory.getMRDAO(EditRecordActivity.this);
					
					if(editEvent.isOld()){
						dao.update(editEvent);
					}else{
						dao.add(editEvent);
					}
					EditRecordActivity.this.finish();
				}
			}
		};

		private static int REMOVE_EVENT = Menu.FIRST;
		@Override
		public boolean onPrepareOptionsMenu(Menu menu) {
			menu.clear();
			if(idET.getVisibility() == View.VISIBLE){
				menu.add(Menu.NONE, REMOVE_EVENT, Menu.NONE, R.string.EditRecordActivity_menu_REMOVE_RECPRD);
			}
			return true;
		}

		@Override
		public boolean onOptionsItemSelected(MenuItem item) {
			DAOFactory.getMRDAO(this).remove(Long.valueOf(idET.getText().toString()));
			Toast.makeText(this, R.string.EditRecordActivity_toast_DELETE_OK, Toast.LENGTH_SHORT).show();
			this.finish();
			return true;
		}
		
}
